<template>
  <view class="container">
    <unicloud-db ref="udb" v-slot:default="{data, pagination, loading, hasMore, error}" :collection="collectionList"
      field="article_id,user_id,comment_content,comment_state,comment_type,reply_user_id,reply_comment_id,comment_date,comment_ip,province">
      <view v-if="error">{{error.message}}</view>
      <view v-else-if="data">
       <uni-list>
          <uni-list-item v-for="(item, index) in data" :key="index" showArrow :clickable="true"
            @click="handleItemClick(item.article_id[0]._id)">
            <template v-slot:body>
              <uni-section class="mb-10" :title="'标题：'+item.article_id[0]?.title"
                :sub-title="'作者：'+ item.user_id[0].username"></uni-section> 
           </template>
          </uni-list-item>
        </uni-list> 
      </view>
      <!-- <uni-load-more :status="loading?'loading':(hasMore ? 'more' : 'noMore')"></uni-load-more> -->
    </unicloud-db>
 <!-- <uni-fab ref="fab" horizontal="right" vertical="bottom" :pop-menu="false" @fabClick="fabClick" /> -->
  </view>
</template>

<script>
  const db = uniCloud.database()
  export default {
    data() {
      return {
        collectionList: [
          db.collection("pyq_comment").where(`comment_state == true && user_id == $cloudEnv_uid `).getTemp(),
          db.collection("pyq_article").field("_id,title,user_id").getTemp(),
          db.collection("uni-id-users").field("_id,username,nickname").getTemp()
        ],
        loadMore: {
          contentdown: '',
          contentrefresh: '',
          contentnomore: ''
        }
      }
    },
    onPullDownRefresh() {
      this.$refs.udb.loadData({
        clear: true
      }, () => {
        uni.stopPullDownRefresh()
      })
    },
    onReachBottom() {
      this.$refs.udb.loadMore()
    },
    methods: {
      handleItemClick(id) {
        uni.navigateTo({
          url: "/pages/detail/detail?id=" + id
        })
      },
      fabClick() {
        // 打开新增页面
        uni.navigateTo({
          url: './add',
          events: {
            // 监听新增数据成功后, 刷新当前页面数据
            refreshData: () => {
              this.$refs.udb.loadData({
                clear: true
              })
            }
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .container {
    :deep(.uni-list-item__container) {
      padding: 0;
    }
  }
</style>
